home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Add-Ons / BBEdit / MacBob 1.0ß2 / Source / Bob / Bob.cp next >
Encoding:
Text File  |  1995-12-12  |  2.9 KB  |  171 lines  |  [TEXT/KAHL]

  1. /***
  2.   *
  3.   *    Bob.cp - the main routine
  4.   *
  5.   *    Original code: Copyright (c) 1991, by David Michael Betz.  All rights reserved
  6.   *    Modifications and additions: Copyright © by Christopher E. Hyde, 1995
  7.   *
  8.   ***/
  9.  
  10. #include "Bob.h"
  11.  
  12. // global variables
  13. char**    bobargv;
  14. int        bobargc;
  15.  
  16. // forward declarations
  17. static void    AddStream    (KStr name, CStream& stream, TValue& pval);
  18. //static TValue    AddStream    (KStr name, CStream& stream);
  19.  
  20.  
  21. static char* pBuf;
  22. static bool pInfo;
  23.  
  24.  
  25. // The main Bob entry point
  26. void
  27. BobMain (void)
  28. {
  29.     pBuf = (char*) Calloc(1024, 1);
  30.  
  31.     pInfo = Opt(VCompile) || Opt(DumpCode) || Opt(TraceExec);
  32.  
  33.         // display the banner
  34.     if (pInfo) {
  35.         gOutput.Put(
  36.             "Bob v1.5.1 - Copyright © 1991, by David Michael Betz."
  37.                 "  All rights reserved.\r"
  38.             "MacBob 1.0ß2 - by Christopher E. Hyde, 1995.\r\r");
  39.     }
  40.  
  41.         // initialize
  42.     pInfo = Opt(VCompile);
  43.     Initialize(kMaxStack);
  44.     InitCompiler();
  45.     bobargc = 0;
  46.     bobargv = nil;
  47.  
  48.         // setup the standard i/o streams
  49. #if 1
  50.     AddStream("stdin",  gInput,  stdin_iostream);
  51.     AddStream("stdout", gOutput, stdout_iostream);
  52.     AddStream("stderr", gOutput, stderr_iostream);
  53. #else
  54.     stdin_iostream  = AddStream("stdin",  gInput);
  55.     stdout_iostream = AddStream("stdout", gOutput);
  56.     stderr_iostream = AddStream("stderr", gOutput);
  57. #endif
  58.  
  59.     CompileDefinitions(gInput);
  60.     pInfo = Opt(VExec);
  61.     Execute("main");
  62. }
  63.  
  64.  
  65. #if 0
  66. extern "C" void Abort (void);
  67.  
  68. void
  69. Abort (void)
  70. {
  71.     Fail(errUnknown);
  72. }
  73.  
  74.  
  75. // Compile definitions in a file
  76. static void
  77. CompileFile (KStr name)
  78. {
  79.     FILE* ifp = fopen(name, "r");
  80.     if (ifp != nil) {
  81.         CompileDefinitions(fgetc, ifp);
  82.         fclose(ifp);
  83.     }
  84. }
  85. #endif
  86.  
  87.  
  88. // Display progress information
  89. void
  90. Info (KStr fmt, ...)
  91. {
  92.     if (pInfo) {
  93.         char* s = pBuf;
  94.         *s++ = '[';
  95.         *s++ = ' ';
  96.  
  97.         s += vsprintf(s, fmt, __va(fmt));
  98.         *s++ = ' ';
  99.         *s++ = ']';
  100.         *s++ = '\r';
  101.         *s = '\0';
  102.         gOutput.Put(pBuf);
  103.     }
  104. }
  105.  
  106.  
  107. // Print a formatted message to the gOutput stream and flush it immediately
  108. void
  109. PrintErrF (KStr fmt, ...)
  110. {
  111.     vsprintf(pBuf, fmt, __va(fmt));
  112.  
  113.     gOutput.Put(pBuf);
  114. }
  115.  
  116.  
  117. // Print a formatted message to the gOutput stream
  118. void
  119. PrintF (KStr fmt, ...)
  120. {
  121.     vsprintf(pBuf, fmt, __va(fmt));
  122.  
  123.     gOutput.Put(pBuf);
  124. }
  125.  
  126.  
  127. // Print an error message and exit
  128. void
  129. Error (KStr fmt, ...)
  130. {
  131. #if 0
  132.     char buf1[100];
  133.     va_list args;
  134.  
  135.     va_start(args, fmt);
  136.     vsprintf(buf1, fmt, args);
  137.     va_end(args);
  138.     PrintErrF("Error: %s\r", buf1);
  139. #else
  140. #define    buf1        &pBuf[16]
  141.     vsprintf(buf1, fmt, __va(fmt));
  142.     PrintErrF("Error: %s\r", buf1);
  143. #endif
  144.  
  145.     Fail(errPrintedMessage);
  146. }
  147.  
  148.  
  149. // Add a built-in I/O stream
  150. #if 1
  151. static void
  152. AddStream (KStr name, CStream& stream, TValue& pval)
  153. {
  154.     extern TValue    symbols;
  155.  
  156.     Entry sym = AddEntry(&symbols, name, stSData);
  157.     set_iostream(&sym->fValue, NewIOStream(stream));
  158.     pval = sym->fValue;
  159. }
  160. #else
  161. static TValue
  162. AddStream (KStr name, CStream& stream)
  163. {
  164.     extern TValue    symbols;
  165.  
  166.     Entry sym = AddEntry(&symbols, name, stSData);
  167.     set_iostream(&sym->fValue, NewIOStream(stream));
  168.     return sym->fValue;
  169. }
  170. #endif
  171.